package com.yksj.healthtalk.comm;

import android.content.Intent;
import android.os.Bundle;
import android.text.Editable;
import android.text.InputFilter;
import android.text.TextWatcher;
import android.text.method.DigitsKeyListener;
import android.view.KeyEvent;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.WindowManager;
import android.widget.EditText;
import android.widget.TextView;
import android.widget.TextView.OnEditorActionListener;

import com.yksj.healthtalk.services.CoreService;
import com.yksj.healthtalk.ui.R;
import com.yksj.healthtalk.utils.DialogUtils;
import com.yksj.healthtalk.utils.HStringUtil;
import com.yksj.healthtalk.utils.SystemUtils;
import com.yksj.healthtalk.utils.ToastUtil;
import com.yksj.healthtalk.utils.WheelUtils;
/**
 * 说明
 * @author Administrator
 * intent.putExtra("title", value);  
 * intent.putExtra("mContentCount", value);  //字数限制  默认是200
 * intent.putExtra("content", content);  //内容
 * 
 * 返回 内容
 * Intent intent=getIntent();
	intent.putExtra("content", value);
	setResult(RESULT_OK, intent);
	finish();
 */
public class CommonExplainActivity extends BaseFragmentActivity implements OnClickListener{
	private EditText mEditText;
	private TextView textNum;
	private int mContentCount=200;//字数限制
	private int textNumber = 0;
	@Override
	protected void onCreate(Bundle arg0) {
		super.onCreate(arg0);
		setContentView(R.layout.common_base_explain);
		getWindow().setSoftInputMode(WindowManager.LayoutParams.SOFT_INPUT_ADJUST_RESIZE);
		initView();
		if (getIntent().getStringExtra("title").equals("取消预约原因")) {
			findViewById(R.id.message).setVisibility(View.VISIBLE);
		}
		if (getIntent().getStringExtra("title").contains("手机")) {
			String digits = "1234567890";
			mEditText.setKeyListener(DigitsKeyListener.getInstance(digits));   
		}
	}
	@Override
	protected void onStart() {
		super.onStart();
		if(getIntent().getStringExtra("title").equals("取消预约原因")){
		Intent intent =new Intent(this,CoreService.class);
		intent.setAction(CoreService.ACTION_FRAME_VIEW_VISIABLE);
		intent.putExtra("className",this.getClass().getName());
		startService(intent);}
	}
	private void initView() {
		initTitle();
		titleLeftBtn.setOnClickListener(this);
		titleRightBtn2.setOnClickListener(this);
		titleRightBtn2.setVisibility(View.VISIBLE);
		titleRightBtn2.setText("确定");
		titleTextV.setText(getIntent().getStringExtra("title"));
		mContentCount=getIntent().getIntExtra("mContentCount", 200);
		textNum = (TextView)findViewById(R.id.textcount);
		textNum.setText("0/"+mContentCount);
		mEditText = (EditText)findViewById(R.id.setting_feedback_text);
		mEditText.addTextChangedListener(textWatcher); 
		mEditText.setFilters(new InputFilter[]{new InputFilter.LengthFilter(mContentCount)});
		if(getIntent().hasExtra("content")){
			String str=getIntent().getStringExtra("content");
			if(!"".equals(str))
			mEditText.setText(str);
		}
		
		//如果是公告板回复,就显示留言说明
		if(getIntent().hasExtra("message_leave")){
			findViewById(R.id.message_leave).setVisibility(View.VISIBLE);
		}
	}

	
	/**
	 * 文字监听
	 */
	private TextWatcher textWatcher = new TextWatcher() {  
        @Override    
        public void afterTextChanged(Editable s) { 
        	
        }   
        @Override 
        public void beforeTextChanged(CharSequence s, int start, int count,  
                int after) {  
        }  
         @Override    
        public void onTextChanged(CharSequence s, int start, int before,     
                int count) {   
      	 if((start +count)<=mContentCount && mEditText.getText().toString().length()<=mContentCount){
        		 textNumber = mEditText.getText().toString().length();
        		 textNum.setText(textNumber+"/"+mContentCount); 
        	 }else{
        		 mEditText.setText(s.subSequence(0, mContentCount));
        		 ToastUtil.showShort(CommonExplainActivity.this, "最多可输入"+mContentCount+"个字符");
        	 }
        }                     
    };  
    
	@Override
	public void onClick(View v) {
			switch (v.getId()) {
			case R.id.title_back:
				SystemUtils.hideSoftBord(this,mEditText);
				finish();
				break;
				
			case R.id.title_right2:
				submitFeedback();
				break;
			}
		}
	
	/**
	 * 提交意见内容
	 */
	private void submitFeedback(){
		WheelUtils.hideInput(CommonExplainActivity.this,mEditText.getWindowToken());
		final String value = mEditText.getEditableText().toString();
//		if(HStringUtil.isEmpty(value)){
//			SingleBtnFragmentDialog.showDefault(getSupportFragmentManager(), "不能为空");
//			return;
//		}
		Intent intent=getIntent();
		intent.putExtra("content", value);
		setResult(RESULT_OK, intent);
		finish();
	}

}
